package studentinformationmanagementsystem.servlet.admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.UUID;

//设置默认上传文件夹为：D:/studentinformationmanagement/
//指定缓存大小：1024 * 1024，单位:bytes  1M
//单个上传文件最大大小:1024 * 1024 * 5，单位:bytes 5M
//限制该multipart/form-data请求中数据的大小,默认是-1，表示没有限制，单位:bytes:1024 * 1024 * 5 * 5
@MultipartConfig(location = "D:/studentinformationmanagement/", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
@WebServlet("/admin/function/upload.do")
public class FileUploads extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String studentid = request.getParameter("studentid");
        String name = request.getParameter("name");
        String idnumber = request.getParameter("idnumber");
        String sex = request.getParameter("sex");
        String password = request.getParameter("password");
        Date birthdate = Date.valueOf(request.getParameter("birthdate"));
//        Integer classandgradeid = Integer.valueOf(request.getParameter("classandgradeid"));
        String classandgradeid = request.getParameter("classandgradeid");
        String nation = request.getParameter("nation");
        Date admitionDate = Date.valueOf(request.getParameter("admitionDate"));
        String phone = request.getParameter("phone");
//        Integer departmentid = Integer.valueOf(request.getParameter("departmentid"));
        String departmentid = request.getParameter("departmentid");
        String politicsstatus = request.getParameter("politicsstatus");
        String photograph = request.getParameter("photograph");
        String homeaddress = request.getParameter("homeaddress");
//        Integer major = Integer.valueOf(request.getParameter("major"));
        String major = request.getParameter("major");
        String mailbox = request.getParameter("mailbox");
        String nativeplace = request.getParameter("nativeplace");
        String emergencycontact = request.getParameter("emergencycontact");
        String emergencycontactphone = request.getParameter("emergencycontactphone");
        String filename = request.getParameter("filename");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        try {
            Part part = req.getPart("file");
            String realFileName = part.getSubmittedFileName();
            String type = realFileName.substring(realFileName.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + type;
            //获取项目下的该文件夹路径，必须先创建文件夹
            String path = "D:/studentinformationmanagement/" + fileName;
            part.write(path);
            //将文件名信息回传至前端
            writer.write(fileName);
        } catch (Exception e) {
            writer.write(e.getMessage());
        }

    }
}
